# coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest
from mock.models import API, Router, Response
from mock import util
import mock.views as mviews
import json


def checkMethod(request):
    if request.method == 'GET':
        id = request.GET.get("user")
        print(id)
        return(id)
    elif request.method == 'POST':
        code = request.POST.get("STATUSCODE")
        return('POST方法！！！')


def checkUrl(request, api, router):
    """
    校验请求的路径是否合法
    :param api:
    :param router:
    :return str:
    """
    print('request:' + request.scheme + '://' +
          request.get_host() + '/mock/' + api + '/' + router)
    # print(request.check(request))

    postBody = request.body
    if len(postBody) == 0:
        print('requestBody 长度为零！！！')
        return 'requestError'
    try:
        requestBody = json.loads(postBody)
        print('requestBody :', requestBody)
    except ValueError:
        return 'jsonErrorRequest'

    if util.isTextEmpty(api) or util.isTextEmpty(router):
        print('api或router为空')
        return 'emptyErrorRequest'

    apiList = API.objects.filter(path=api)

    if len(apiList) == 0:
        print('api服务不存在')
        return 'apiErrorResponse'

    routerList = Router.objects.filter(api=apiList[0], path=router)

    if len(routerList) == 0:
        print('router方法不存在')
        return 'routerErrorResponse'

    responseList = Response.objects.filter(router=routerList[0])

    if len(responseList) == 0:
        print('responseList未配置！')
        return 'responseErrorResponse'
    return True
